package client;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import comm.ClientComm;
import comm.ClientInterface;
import comm.ClientSideMessage;

@SuppressWarnings("serial")
public class Client extends JFrame implements MoodishClient{
	JTextArea textArea;
	JTextField textField;
	JButton okButton;
	String username;
	ClientInterface clientComm;
	private ClientSideMessage msgReceived = null;

	public void init() {
		//User Interface:
		//textArea 		Box where the messages are displayed
		//textField 	The text field where the user writes
		//okButton		Push ok to send message
		this.setLayout(new BorderLayout());
		textArea=new JTextArea(5,10);
		textField=new JTextField(20);
		okButton=new JButton("SEND");
		username=JOptionPane.showInputDialog("Username :");

		this.add(textArea,BorderLayout.CENTER);
		this.add(textField,BorderLayout.NORTH);
		this.add(okButton,BorderLayout.PAGE_END  );

		//Creates Interface
		clientComm = new ClientInterface();
		start(clientComm);	
		
		okButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				clientComm.sendMoodishMessage(new String(textField.getText()));
				System.out.println("@"+username + ": sends msg!!" + textField.getText());
				textField.setText("");
			}
		});	

	
		setSize(300,300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);

	}

	public ClientComm getClientComm(){
		return clientComm;
	}

	@Override
	public void start(ClientComm clientComm) {
		String printOnScreen = "";
		boolean estado = startConnection();
		//if(startConnection() == true){
			// Frame definitions
			
			while(clientComm.isConnected()){
				if(clientComm.hasNextMessage()){
					// *** DISPLAYING MSGS RECEIVED ***
					// *** DISPLAYING MSGS RECEIVED ***
					msgReceived = clientComm.getNextMessage();
					System.out.println(this.username+"@New msg received@ " + " from "+ msgReceived.getSendersNickname()
							 + " payload="+msgReceived.getPayload() + " type="+msgReceived.getType());
					System.out.println(msgReceived);
					//New User Connects
					if(msgReceived.getType() == ClientSideMessage.Type.CONNECTED){
						printOnScreen = new String(" your are connected!");
						textArea.setText(textArea.getText()+"\n"+printOnScreen);
					}
					//User Disconnects
					if(msgReceived.getType() == ClientSideMessage.Type.DISCONNECTED){
						printOnScreen = new String(msgReceived.getSendersNickname() + " logs Off!");
						textArea.setText(textArea.getText()+"\n"+printOnScreen);
					}
					//Moodish Message
					if (msgReceived.getType() == ClientSideMessage.Type.MOODISH_MESSAGE){
						printOnScreen = new String(msgReceived.getSendersNickname() + " says " + msgReceived.getPayload());
						textArea.setText(textArea.getText()+"\n"+printOnScreen);
					}
					
				}

			//}
		}
		//else System.out.println("Connection is down!");
	}

	//Establish the connection
	private boolean startConnection(){
		if(!clientComm.isConnected()){
			try {
				clientComm.connect("localhost", username);
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return clientComm.isConnected();
	}

	//Starting client(s)
	public static void main(String[] args){
		Client client1 = new Client();
		Client client2 = new Client();
		client1.init();
		client2.init();
		
		
		
	}
	


}
